<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	Import::AppLib("word");
	class FeedbackController extends Controller {
		public function index(){
			$start = Basic::getVar("start");
			$perpage = Basic::getVar("perpage");
			$search = Basic::getVar("search");
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			$searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
			$sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"created";
			$sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
			$article = $this->loadModel("Feedback");
			$articleRecords = $article->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
			$totalRecords = $article->getTotalRecords();
			if ( $start < $totalRecords ){
				$this->set("totalRows",$totalRecords);
			}elseif ( $start >= $totalRecords ) {
				$start = $start-20;
				$articleRecords = $article->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
				$this->set("totalRows",$totalRecords);
			}
			$this->set("currentPage",$start);
			$this->set("perpage",$perpage);
			$this->set("search",$search);
			$this->set("searchColumns",$searchColumns);
			$this->set("sortColumn",$sortColumn);
			$this->set("sortCriteria",$sortCriteria);
			$this->set("search",$search);
			$this->set("articles",$articleRecords);
			$this->set("selectedMenu","FEE");
			$this->set( "title" , "SMECA - Article Feedback List" );
			$this->setLanguage( "English" );
			$this->renderDefault("display","feedback");
		}	
		public function edit(){
			$id = Basic::getVar("id");
			$feedback = $this->loadModel("Feedback");
			$feedback = $feedback->getFeedback($id);			
			$this->set('feedback', $feedback[0]);
			$this->set("selectedMenu","FEE");
			$this->set( "title" , "SMECA - Article Feedback Reply" );
			$this->setLanguage( "English" );
			$this->renderDefault("edit","feedback" );
		}
		public function replyprocess(){
			$feedback = $this->loadModel("Feedback");
			$this->set("info","Comment is reply successfully.");
			
			
			$user = SESSION::get("user");

			$_POST['data']['az_article_feedbacks']['replied_by'] = $user->id;
			$_POST['data']['az_article_feedbacks']['reply'] = 1;			
			$feedback->replyComment( $_POST['data'],$_POST['data']['az_article_feedbacks']['status']);
			
			$id = $_POST['data']['az_article_feedbacks']['id'];
			$feedback = $feedback->getFeedbackRef($id);

			$ref = $feedback[0]->ref_number;
			$message ="
	                <html>
	                <head>
	                <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
	                </head>
	                <body>";
			if($_POST['data']['az_article_feedbacks']['status'] != 0){ // could be as a private or public comment
				$subject = "Report of Member's Comments";
				$message .= "
			                    <div style='width:680px;margin-bottom:15px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:11px;'>
			                        <div align='center'><img src='http://smecaclinic.com/web/public/img/logo.png' /> </div></div>
			                    </div>
			                    <p  style='width:680px;margin:0 0 5px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:12px;font-weight:bold'>
			                        Date &amp; Time : " . Date("D, Y-m-d h:i:s A") . "
			                    </p>
			                    <p  style='width:680px;margin:0 0 5px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:12px;font-weight:bold'>
			                        Reference Number : " . $ref . "
			                    </p>
			                    <table style='FONT-FAMILY:Cambria,Arial;BORDER-COLLAPSE:collapse' cellspacing='0' cellpadding='5' width='680' align='left' border='0'>
			                        <tbody>
			                        <tr>
			                        </tr>
			                        <tr valign='top'>
			                            <td style='FONT-SIZE:11px' colspan='2' >
			                        		<p>
											Dear Member,<br><br>

											Smeca Clinic takes its member's comments seriously.<br><br>
											
											Smeca Clinic is an independent entity which commits to the development of individuals and businesses.<br><br>
											
											We appreciate your contributions and look forward to seeing more from your participations.<br><br>
											
											Sincerely Yours,<br><br><br>
														                        		
			           
			           
											General Administration<br>
											Smeca Clinic<br>
											Tel: (61) 414 282 552 / 403 699 334<br>
											Email: admin@smecaclinic.com<br>
											Page: <a href='http://www.smecaclinic.com'>http://www.smecaclinic.com</a>
                
			                        		</p>
			                           	</td>
			                        </tr>
			                        </tbody>
			                    </table>
			                ";
                
                $message .="</body></html>";
			}elseif($_POST['data']['az_article_feedbacks']['status'] == 0){ // block that comment or delete
				$subject = "Disapproval of Member's Comments";
				$message .= '
			                    <div style="margin-bottom:15px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:11px;">
			                        <div align="center"> <img src="' . BASE_URL . IMG_FOLDER. 'logo.png" /> </div>
			                    </div>
			                    <p  style="width:680px;margin:0 auto 5px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:12px;font-weight:bold">
			                        Date &amp; Time : ' . Date('D, Y-m-d h:i:s A') . '
			                    </p>
			                    <p  style="width:680px;margin:0 auto 5px;FONT-FAMILY:Cambria,helvetica,sans-serif;font-size:12px;font-weight:bold">
			                        Reference Number : ' . $ref . '
			                    </p>
			                    <table style="FONT-FAMILY:Cambria,Arial;BORDER-COLLAPSE:collapse" cellspacing="0" cellpadding="5" width="680" align="left" border="0">
			                        <tbody>
			                        <tr>
			                        </tr>
			                        <tr valign="top">
			                            <td style="FONT-SIZE:11px" colspan="2" >
			                        		<p>
											Dear Member,<br><br>

											Smeca Clinic takes its member\'s comments seriously.<br><br>
											
											Smeca Clinic is an independent entity which commits to the development of individuals and businesses.<br><br>
											
											Therefore, comments will be removed from our website if we view them as inappropriate for our core businesses.<br><br>
											
											Our apology for any inconveniences caused.<br><br>
											
											Sincerely Yours,<br><br><br>
														                        		
				
				
				
											General Administration<br>
											Smeca Clinic<br>
											Tel: (61) 414 282 552 / 403 699 334<br>
											Email: admin@smecaclinic.com<br>
											Page: <a href="http://www.smecaclinic.com">http://www.smecaclinic.com</a>
				
			                        		</p>
			                           	</td>
			                        </tr>
			                        </tbody>
			                    </table>
			                ';
				
				$message .='</body></html>';
			}
			//die($message);
			/*require_once(VENDOR_FOLDER . "PHPMailer/class.phpmailer.php");
			//Import::vendor("PHPMailer/class.phpmailer.php");
			
			$mail = new PHPMailer();
			
			$mail->IsSMTP();                   
			$mail->Host = "localhost";  // specify main and backup server
			
			$mail->From = "admin@smecaclinic.com";
			$mail->FromName = "SMECA CLINIC";
			$mail->AddAddress($_POST['email']);
			
			$mail->WordWrap = 50;                                 // set word wrap to 50 characters
			$mail->IsHTML(true);                                  // set email format to HTML
			//$mail->AddEmbeddedImage(BASE_URL . IMG_FOLDER . 'logo.png', 'logoimg', 'logo.png'); // attach file logo.jpg, and later link to it using identfier logoimg
			$mail->Subject = $subject;
			$mail->Body    = $message;
			
			if(!$mail->Send())
			{
				echo "Message could not be sent. <p>";
				echo "Mailer Error: " . $mail->ErrorInfo;
				exit;
			}
			
			echo "Message has been sent";
			*/	
			
			//---------------------------------------------------------
			// To send HTML mail, the Content-type header must be set |
			//---------------------------------------------------------
			$header  = 'MIME-Version: 1.0' . "\r\n";
			$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";			
			//---------------------
			// Additional headers |
			//---------------------			
			$to = $_POST['email'];
			$name = "SMECA CLINIC";
			$header .= 'From:' . $name . " <admin@smecaclinic.com>\r\n";
			//Just add a Simple hack here to show the SMTP conversation
			
			if(mail($to, $subject, $message, $header)){			
				$this->redirect( array("com"=>"feedback","action"=>"index"));
			}else{
				$this->set("info","Replying comment was failed, please try again.");
				$this->redirect( array("com"=>"feedback","action"=>"index"));
			}
		}
		public function delete(){
			$ids = Basic::getVar("id");
			$user = $this->loadModel("Feedback");
			if ( is_array($ids)&&!empty($ids) ){
				$this->set("info","Article Feedback(s) delete successfully.");
				$user->delete( $ids );
			}else{
				$this->set("info","Failed to delete article feedback(s) successfully.");
			}
			$this->redirect( array("com"=>"feedback","action"=>"index"));
		}
	}